package www.com.library.view;

import android.graphics.Canvas;

import java.util.LinkedList;
import java.util.List;

/**
 * 文字组装类
 * @author Administrator
 *
 */
public class UISpannableString {
	/**
	 * 文字颜色值控制器
	 * @author Administrator
	 *
	 */
	public static class UiColorSpan {
		public int mStart;
		public int mEnd;
		public int mColor;
		public String mText;

		public UiColorSpan copy(UiColorSpan right) {
			mStart = right.mStart;
			mEnd = right.mEnd;
			mColor = right.mColor;
			mText = right.mText;
			return this;
		}
	}

	private static final int MAX_COLOR_SPANS = 16;
	private String mText;
	private List<UiColorSpan> mUsedColorSpans;
	private List<UiColorSpan> mFreeColorSpans;

	public UISpannableString() {
		mUsedColorSpans = new LinkedList();
		mFreeColorSpans = new LinkedList();
	}

	public boolean isEmpty() {
		if (mText == null || mText.isEmpty())
			return true;

		return false;
	}

	public synchronized void clear() {
		int count = 0;
		for (UiColorSpan span : mUsedColorSpans) {
			mFreeColorSpans.add(span);
			count++;
			if (count >= MAX_COLOR_SPANS)
				break;
		}
		mUsedColorSpans.clear();
		mText = "";
	}

	public synchronized void append(String text, int color) {
		if (text == null || text.isEmpty())
			return;

		final int start = mText.length();
		final int end = start + text.length() - 1;

		UiColorSpan span;
		if (mFreeColorSpans.size() > 0) {
			span = mFreeColorSpans.get(0);
		} else {
			span = new UiColorSpan();
			mFreeColorSpans.add(span);
		}
		mFreeColorSpans.remove(span);

		span.mColor = color;
		span.mStart = start;
		span.mEnd = end;
		span.mText = text;
		mUsedColorSpans.add(span);
		mText += text;
	}

	public String getText() {
		return mText;
	}

	public void draw(Canvas canvas, float x, float y, UITextPaint paint) {
		if (mUsedColorSpans.size() <= 0) {
			paint.draw(canvas, mText, x, y);
			return;
		}

		float xOff = x;
		final UiColorSpan firstSpan = mUsedColorSpans.get(0);
		if (firstSpan != null) {
			if (firstSpan.mStart != 0) {
				String startStr = mText.substring(0, firstSpan.mStart);
				paint.draw(canvas, startStr, xOff, y);
				xOff = paint.measureText(startStr);
			}
		}

		for (UiColorSpan span : mUsedColorSpans) {
			paint.setColor(span.mColor);
			paint.draw(canvas, span.mText, xOff, y);
			xOff += paint.measureText(span.mText);
		}
	}

	public float autoScaleDraw(Canvas canvas, float x, float y,
			float expectedWidth, UITextPaint paint) {
		if (mUsedColorSpans.size() <= 0) {
			return paint.autoScaleDraw(canvas, mText, x, y, expectedWidth);
		}

		float remainWidth = expectedWidth;
		float xOff = x;
		final UiColorSpan firstSpan = mUsedColorSpans.get(0);
		if (firstSpan != null) {
			if (firstSpan.mStart != 0) {
				String startStr = mText.substring(0, firstSpan.mStart);
				xOff = paint.autoScaleDraw(canvas, startStr, xOff, y,
						remainWidth);
				remainWidth -= xOff;
			}
		}

		for (UiColorSpan span : mUsedColorSpans) {
			paint.setColor(span.mColor);
			final float width = paint.autoScaleDraw(canvas, span.mText, xOff,
					y, remainWidth);
			xOff += width;
			remainWidth -= width;
		}

		return expectedWidth - remainWidth;
	}
}
